package com.youdianstar.app.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.R
import com.youdianstar.app.databinding.LayoutItemRecordBinding
import com.youdianstar.app.entity.PaymentRecord
import com.youdianstar.app.ui.pay.BillDetailActivity
import com.youdianstar.app.common.Extra
import com.youdianstar.app.common.DataCode
import com.youdianstar.app.common.ex.toJson

import com.youdianstar.app.common.util.DateUtil
import com.youdianstar.app.common.util.MathUtil
import com.youdianstar.app.common.util.StrUtil
import com.youdianstar.app.util.DataAppManager
import com.youdianstar.app.common.auko.startActivity


/**
 * File: RecordPagingAdapter
 * Author: 82149 Create: 2021/11/27 20:30
 * Changes (from 2021/11/27)
 * --------------------------------------------------
 * description: 交易流水记录
 * ---------------------------------------------------
 *
 */
class TransPagingAdapter : PagingDataAdapter<PaymentRecord, RecyclerView.ViewHolder>(differCallback){
    companion object{
        val TAG  = "OrderPagingAdapter"
        val differCallback = object : DiffUtil.ItemCallback<PaymentRecord>(){
            override fun areItemsTheSame(oldItem: PaymentRecord, newItem: PaymentRecord): Boolean {
                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(oldItem: PaymentRecord, newItem: PaymentRecord): Boolean {
                return oldItem == newItem
            }

        }
    }

    private var symbolTx = ""

    init {
        symbolTx = DataAppManager.instance.symbolTx()
    }

    internal class RecordVH(val binding: LayoutItemRecordBinding) :RecyclerView.ViewHolder(binding.root){

        fun bindData(item : PaymentRecord,symbolTx:String){
            //流水名称
            when(item.type){
                DataCode.BisType_Rent->{
                    binding.ivRefund.visibility = View.INVISIBLE
                    binding.tvName.text = binding.root.context.getString(R.string.wallet_rental)
                }
                DataCode.BisType_Rent_Refund->{
                    binding.ivRefund.visibility = View.VISIBLE
                    binding.tvName.text = binding.root.context.getString(R.string.wallet_rental)
                }
                DataCode.BisType_Deposit->{
                    binding.ivRefund.visibility = View.INVISIBLE
                    binding.tvName.text = binding.root.context.getString(R.string.wallet_deposit)
                }
                DataCode.BisType_Deposit_Rf->{
                    binding.ivRefund.visibility = View.VISIBLE
                    binding.tvName.text = binding.root.context.getString(R.string.wallet_deposit)
                }
                DataCode.BisType_Penalty->{
                    binding.ivRefund.visibility = View.INVISIBLE
                    binding.tvName.text = binding.root.context.getString(R.string.pay_powerbank)
                }
                DataCode.BisType_Penalty_Rf->{
                    binding.ivRefund.visibility = View.VISIBLE
                    binding.tvName.text = binding.root.context.getString(R.string.pay_powerbank)
                }
                DataCode.BisType_Coupon->{
                    binding.ivRefund.visibility = View.INVISIBLE
                    binding.tvName.text = binding.root.context.getString(R.string.pay_coupon)
                }
                DataCode.BisType_Coupon_Rf->{
                    binding.ivRefund.visibility = View.VISIBLE
                    binding.tvName.text = binding.root.context.getString(R.string.pay_coupon)
                }

                DataCode.BisType_Charging->{
                    binding.ivRefund.visibility = View.INVISIBLE
                    binding.tvName.text = binding.root.context.getString(R.string.charge_card)
                }
                DataCode.BisType_Charging_Rf->{
                    binding.ivRefund.visibility = View.VISIBLE
                    binding.tvName.text = binding.root.context.getString(R.string.charge_card)
                }
            }
            //支付方式
            when(item.paymentMethod){
                DataCode.Type_Card,DataCode.Type_NewCard->{
                    if (StrUtil.isEmpty(item.bankNo)){
                        binding.tvDes.text = binding.root.context.getString(R.string.wallet_type_bankcard)
                    }else{
                        binding.tvDes.text = binding.root.context.getString(R.string.wallet_type_bankcard)+" ("+item.bankNo+")"
                    }
                }
                DataCode.Type_Google->{
                    binding.tvDes.text = binding.root.context.getString(R.string.wallet_type_googlepay)
                }
                DataCode.Type_Apple->{
                    binding.tvDes.text = binding.root.context.getString(R.string.wallet_type_applepay)
                }
                else -> {
                    binding.tvDes.text = "--"
                }
            }
            //金额
            binding.tvAmount.text = "-$symbolTx ${MathUtil.round(item.amount)}"
            //日期
            binding.tvTime.text = DateUtil.getOrderTime(item.createTime)
            //状态标识
            binding.ivRefund.visibility = if (item.refund || item.paymentState>2) View.VISIBLE else View.GONE //退款状态Icon
        }

    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        getItem(position)?.let {
            (holder as RecordVH).bindData(it,symbolTx)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val recordItem = RecordVH(LayoutItemRecordBinding.inflate(LayoutInflater.from(parent.context), parent, false))
        recordItem.itemView.setOnClickListener {
            //跳转详情
            val data = getItem(recordItem.layoutPosition)
            data?.let { bean->
                parent.context.startActivity<BillDetailActivity>(
                    Extra.IT_ID to bean.transferNo,
                    Extra.IT_DATA to bean.toJson()
                )
            }
        }
        return recordItem
    }


}